package com.demo.lock.jdk;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class LockDemo {

    private ReadWriteLock rwl = new ReentrantReadWriteLock();

    public static void main(String[] args) {
        final LockDemo lockDemo = new LockDemo();
        new Thread(() -> lockDemo.testRwl(Thread.currentThread())).start();
        new Thread(() -> lockDemo.testRwl(Thread.currentThread())).start();
    }


    public void testRwl(Thread thread) {
        rwl.readLock().lock();
        try {
            long finish = System.currentTimeMillis() + 1;
            while (System.currentTimeMillis() <= finish) {
                System.out.println(thread.getName() + "正在读");
            }
            System.out.println(thread.getName() + "读取结束");
        } finally {
            rwl.readLock().unlock();
        }
    }


}
